/**
 *
 * 过滤器的过滤项
 *
 * */
import {ICellInputProps} from "@/biz-components/claim-items/components-v2/cell-input";
import React from "react";
import {getLabelFromMetaArr} from "@/biz-components/claim-items/components";

export interface CommonFilterItemProps<T, E> {
	//要过滤的数据的属性
	name: string;
	// 过滤项的输入组件，和组件的额外参数
	Component: [React.ComponentType<ICellInputProps<T> & E> & {defaultValue: T}, E],
	// 数据的匹配方式
	filterFunction: (filterValue: T, dataValue: any) => boolean;
	initialValue?: T;
}

export function CommonFilterItem<T, E>(props: CommonFilterItemProps<T, E>) {

	return null;
}



type CommonFilterItemRenderProps = CommonFilterItemProps<any, any> & {
	onFilterItemMount: (getFilterItemValueFunc: () => any) => void;
	onFilterItemUnMount: () => void;
	metaArr: any[];
	onChange: () => void;
}
/**
 * 渲染Filter Item 的内容的组件
 * */
export class CommonFilterItemRender extends React.Component<CommonFilterItemRenderProps> {

	state: {
		value: any
	};

	constructor(props: CommonFilterItemRenderProps) {
		super(props);
		this.getCurrentValue = this.getCurrentValue.bind(this);

		const initialValue = this.props.initialValue;

		this.state = {
			value: initialValue !== undefined ? initialValue : this.props.Component[0].defaultValue
		}
	}
	componentDidMount() {
		this.props.onFilterItemMount(this.getCurrentValue);
	}
	componentWillUnmount() {
		this.props.onFilterItemUnMount();
	}

	getCurrentValue() {
		return this.state.value;
	}
	render() {
		const {name, Component, metaArr, onChange} = this.props;

		const [C, cProps] = Component;


		return (
			<div className={"filter-item"}>
				<label>{getLabelFromMetaArr(metaArr, name)}</label>
				<C
					{...cProps}
					disabled={false}
					value={this.state.value}
					onChange={(v: any) => {
						this.setState({value: v}, onChange);
					}}
				/>
			</div>
		)
	}
}
